from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.ext.declarative import declarative_base

db = SQLAlchemy()

Base = declarative_base()


class Page:
    def __init__(self, page, size, total, elements):
        total_page = int(total / size)
        if total % size > 0:
            total_page = total_page + 1
        self.page = page
        self.size = size
        self.total = total
        self.elements = elements
        self.has_next = page < total_page
        self.has_pre = page > 1
        self.total_page = total_page


class Archive:
    def __init__(self, count, article, archive):
        self.count = count
        self.article = article
        self.archive = archive


class Label(db.Model):
    __tablename__ = 'e_label'
    uuid = db.Column(db.String(36), primary_key=True)
    label_name = db.Column(db.String(16))
    create_at = db.Column(db.DateTime)
    create_by = db.Column(db.String(64))
    update_at = db.Column(db.DateTime)
    update_by = db.Column(db.String(64))


class LabelRecord(db.Model):
    __tablename__ = 'e_label_record'
    uuid = db.Column(db.String(36), primary_key=True)
    article_uuid = db.Column(db.String(36))
    label_uuid = db.Column(db.String(64))


class User(db.Model):
    __tablename__ = 'e_user'
    uuid = db.Column(db.String(36), primary_key=True)
    account = db.Column(db.String(64))
    real_name = db.Column(db.String(64))
    nick_name = db.Column(db.String(64))
    password = db.Column(db.String(64))
    salt = db.Column(db.String(16))
    email = db.Column(db.String(64))
    sex = db.Column(db.String(8))
    is_admin = db.Column(db.Boolean)
    avatar = db.Column(db.String(256))
    create_at = db.Column(db.DateTime)
    create_by = db.Column(db.String(64))
    update_at = db.Column(db.DateTime)
    update_by = db.Column(db.String(64))
    # new stop normal
    status = db.Column(db.String(8))
    # user type(qq wx github)
    type = db.Column(db.String(8))
    third_part_account = db.Column(db.String(256))


class Article(db.Model, Base):
    __tablename__ = 'e_article'
    uuid = db.Column(db.String(36), primary_key=True)
    summary = db.Column(db.String(255))
    title = db.Column(db.String(255))
    content_url = db.Column(db.String(255))
    pic_url = db.Column(db.String(255))
    status = db.Column(db.String(25))
    is_top = db.Column(db.Boolean)
    has_source = db.Column(db.Boolean)
    view_count = db.Column(db.Integer)
    create_at = db.Column(db.DateTime)
    create_by = db.Column(db.String(36))
    update_at = db.Column(db.DateTime)
    update_by = db.Column(db.String(36))
    update_times = db.Column(db.Integer)
    is_comment = db.Column(db.Boolean)
    is_draft = db.Column(db.Boolean)
    comment_count = db.Column(db.Integer)
    document_type = db.Column(db.String(10))
    group_id = db.Column(db.String(36))
    archive = db.Column(db.String(8))
    content = ""
    labels = []
    up = db.Column(db.Integer)
    down = db.Column(db.Integer)


class FriendLink(db.Model):
    __tablename__ = 'e_friend_link'
    uuid = db.Column(db.String(36), primary_key=True)
    name = db.Column(db.String(255))
    status = db.Column(db.String(8))
    avatar = db.Column(db.String(512))
    url = db.Column(db.String(255))
    description = db.Column(db.String(512))
    create_at = db.Column(db.DateTime)
    create_by = db.Column(db.String(36))
    update_at = db.Column(db.DateTime)
    update_by = db.Column(db.String(36))
    email = db.Column(db.String(64))


class Visit(db.Model):
    __tablename__ = 'e_visit'
    uuid = db.Column(db.String(36), primary_key=True)
    host_ip = db.Column(db.String(255))
    url = db.Column(db.String(255))
    create_at = db.Column(db.DateTime)
    create_by = db.Column(db.String(36))


class Download(db.Model):
    __tablename__ = 'e_download'
    uuid = db.Column(db.String(36), primary_key=True)
    resource_uuid = db.Column(db.String(36))
    create_at = db.Column(db.DateTime)
    create_by = db.Column(db.String(36))


class Resource(db.Model):
    __tablename__ = 'e_resource'
    uuid = db.Column(db.String(36), primary_key=True)
    content_url = db.Column(db.String(255))
    download_times = db.Column(db.String(36))
    create_at = db.Column(db.DateTime)
    create_by = db.Column(db.String(36))


class Comment(db.Model):
    __tablename__ = 'e_comment'
    uuid = db.Column(db.String(36), primary_key=True)
    reference_uuid = db.Column(db.String(36))
    reference_type = db.Column(db.String(8))
    status = db.Column(db.String(8))
    message = db.Column(db.String(512))
    to_reference = db.Column(db.String(512))
    up = db.Column(db.Integer)
    down = db.Column(db.Integer)
    create_at = db.Column(db.DateTime)
    create_by = db.Column(db.String(36))
    update_at = db.Column(db.DateTime)
    update_by = db.Column(db.String(36))


class SystemConfig(db.Model):
    __tablename__ = 'e_system_config'
    uuid = db.Column(db.String(36), primary_key=True)
    key = db.Column(db.String(36))
    value = db.Column(db.String(255))
    description = db.Column(db.String(512))
    create_at = db.Column(db.DateTime)
    create_by = db.Column(db.String(36))
    update_at = db.Column(db.DateTime)
    update_by = db.Column(db.String(36))


class EventLog(db.Model):
    __tablename__ = 'e_event_log'
    uuid = db.Column(db.String(36), primary_key=True)
    event_type = db.Column(db.String(8))
    create_at = db.Column(db.DateTime)
    create_by = db.Column(db.String(36))
